<?php
$config = new RM_Config;

$priceSystem = RM_Prices_Manager::getInstance()->getRealPriceSystem($this->unit);
$dateTimeFormat = $priceSystem->getDateformat();
list($dateFormat, $timeFormat) = explode(' ', $dateTimeFormat);
$phpDateFormat = $priceSystem->getDateformat(true);

$startDay = $config->getValue('rm_config_calendar_startday');
$disabledPeriods = $this->calendardata;

$criteria = RM_Reservation_Manager::getInstance()->getCriteria();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-gb" lang="en-gb" dir="ltr" id="minwidth" >
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    
    <script type="text/javascript">
	RM = {};
	RM.Pages = {};
	RM.Pages.User = {};
	RM.Pages.User.Unit = {};
	RM.Common = {};
	
	RM.BaseURL = '<?php echo RM_Environment::getInstance()->getRouter()->_(); ?>';
	RM.RootURL = '<?php echo RM_Environment::getConnector()->getRootURL(); ?>';
	
	<?php echo $this->_router->getAccembleURLJsCode(); ?>
	</script>
	
	<script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/admin/common.js"></script>
	<script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/admin/lists.js"></script>
	<script type="text/javascript" src="<?php echo RM_Environment::getInstance()->getRouter()->_('Language', 'constants'); ?>"></script>
	
	<!-- ExtJS CSS -->
	<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/resources/css/ext-all.css"/>
	<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/userdata/css/user.css"/>
	
    <!-- ExtJS Base -->
	<?php
    $config = Zend_Registry::get('config');
    if ((int)$config->get('js')->get('debug')->get('user') == 1) {
    ?>
    <script language="javascript" type='text/javascript' src='<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/adapter/ext/ext-base-debug.js'></script>
    <script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/ext-all-debug.js"></script>
    <?php
    } else {
    ?>
    <script language="javascript" type='text/javascript' src='<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/adapter/ext/ext-base.js'></script>
    <script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/ext-all.js"></script>
    <?php
    }
    ?>
    <script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext_addons/ux/Ext.ux.form.DateTime.js"></script>

</head>
<body>




<div class="RM_datepicker_modal_container">
    <div class="RM_datepicker_modal_header">
            <?php echo $this->_translate->_('User.DatePicker','DateSelection');?>
    </div>
    <div class="RM_dateselector">
            <div class="RM_dateselector_from">
                    <div class="RM_dateselector_label"><?php echo $this->_translate->_('StartDate');?></div>
                    <div id="RM_start_dp_calendar"></div>
            </div>            
            <div class="RM_dateselector_to" style="display:none;" >
                    <div class="RM_dateselector_label"><?php echo $this->_translate->_('EndDate');?></div>
                    <div id="RM_end_dp_calendar"></div>
            </div>
        <div id="RM_dateselector_message">
            <?php echo $this->_translate->_('User.DatePicker','HelpMsg');?>
        </div>
            <div id="RM_dateselector_button">
                <input name="" type="button" value="OK" class="button" onclick="RM_dp_Submit()" />
            </div>
    </div>

</div>

<script type="text/javascript">
var RM_dp_submitFormat = '<?php echo $phpDateFormat; ?>';

function RM_dp_Submit(){

    document.getElementById('RM_dateselector_message').innerHTML = "<?php echo $this->_translate->_('User.DatePicker','SavingDates');?>";
    document.getElementById('RM_dateselector_button').style.display = "none";
	
	
	
    var startDate = Ext.getCmp('rm_date_startdate_time').getValue();
    var endDate = Ext.getCmp('rm_date_enddate_time').getValue();
	

	startDate.setHours(10,00,00);
	endDate.setHours(20,00,00);
	
	
    var conn = new Ext.data.Connection();
    var request = {
        url: RM.Common.AssembleURL({
            controller : 'Reservations',
            action: 'setdatecriteriajson'
        }),
        params: {
            unit_id : <?php echo $this->unit->getId();?>,
            startdate: startDate.format(RM_dp_submitFormat),
            enddate: endDate.format(RM_dp_submitFormat)
        },
        method: 'POST',
        success: function(responseObject) {
            var json = Ext.util.JSON.decode(responseObject.responseText)
            if (json.success===true){
                window.parent.location.reload();
            } else {
                document.getElementById('RM_dateselector_message').innerHTML = json.error;
                document.getElementById('RM_dateselector_button').style.display = "block";
            }
        },
        failure: function(){
            document.getElementById('RM_dateselector_message').innerHTML = "<?php echo $this->_translate->_('User.DatePicker','AJAXFailure');?>";
        }
    };
    conn.request(request);
};

Ext.onReady(function(){

    var disabledData = Ext.util.JSON.decode('<?php echo $disabledPeriods;?>');
	

    <?php
    if (is_object($criteria) && $criteria->start_datetime) {
        ?>
        var startDate = new Date(<?php echo strtotime($criteria->start_datetime); ?>000);
        <?php
    } else {
        ?>
        var startDate = new Date();
        startDate.setHours(0);
        startDate.setMinutes(0);
        <?php
    }
    if (is_object($criteria) && $criteria->end_datetime) {
        ?>
        var endDate = new Date(<?php echo strtotime($criteria->end_datetime); ?>000);
        <?php
    } else {
        ?>
        var endDate = new Date();
        endDate.setHours(23);
        endDate.setMinutes(0);
        <?php
    }
    ?>

    var startDatePicker = new Ext.ux.form.DateTime({
        id: "rm_date_startdate_time",
        renderTo: 'RM_start_dp_calendar',
        width: 250,
        dateFormat: "<?php echo $dateFormat; ?>",
        timeFormat: "<?php echo $timeFormat; ?>",
        dateValidator: function(value) {
            Ext.getCmp('rm_date_enddate_time').setValue(this.getValue());
            return true;
        },
        timeConfig: {
            increment: 15
        }
    });
    var endDatePicker = new Ext.ux.form.DateTime({
        id: "rm_date_enddate_time",
        renderTo: 'RM_end_dp_calendar',
        timeConfig: {
            increment: 15
        },
        width: 250,
        dateFormat: "<?php echo $dateFormat; ?>",
        timeFormat: "<?php echo $timeFormat; ?>"
    });

    startDatePicker.setValue(startDate);
    endDatePicker.setValue(endDate);
	document.getElementById('ext-gen8').style.visibility = 'hidden';
});

</script>
</body>
</html>